package com.saas.servlet;

import com.saas.entity.Student;
import com.saas.service.IStudentService;
import com.saas.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/update")
public class UpdateStudentServlet extends HttpServlet {

    private IStudentService iss = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");
        String name = req.getParameter("stu_name");
        String sex = req.getParameter("stu_sex");
        String cid = req.getParameter("c_id");

        Student stu = new Student();

        stu.setStu_id(Integer.parseInt(id));
        stu.setStu_name(name);
        stu.setStu_sex(sex);
        stu.setC_id(Integer.parseInt(cid));

        if (iss.updateStudent(stu)){
            resp.sendRedirect("stus");
        }else {
            resp.sendRedirect("update.jsp");
        }

    }
}
